<?php

/**
 * Flickr Photosets - Ajax Receiver
 * Returns data for ajax calls.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

$method = $_REQUEST['method'];

// Get the fb:iframe tag for the map
// This is so we can get the proper height
if ($method == 'map') {
	
	/*
	This is what we should get...
	
	method => map
	uid => the uid of the photo's owner
	height => the height to set the iframe to
	
	pid => the photo id
	-or-
	sid => the set id
	
	*/
	
	$fp = new FP_Custom();
	$coll_id = ($_REQUEST['sid']) ? '&sid='.$_REQUEST['sid'] : '&pid='.$_REQUEST['pid'];
	echo '<fb:iframe src="'.$fp->callback_url.'/map_frame.php?uid='.$_REQUEST['uid'].$coll_id.'&h='.$_REQUEST['height'].'" scrolling="no" frameborder="0" width="'.$fp->fb_sizes['canvas'].'" height="'.$_REQUEST['height'].'" />';
	
}
// Add a tag to the photo
else if ($method == 'add_tag') {
	
	/*
	These are the things that we should be getting posted...
	
	method => add_tag
	uid => the uid of the photo's owner
	pid => the photo id
	token => sha512(owner_uid.pid.secret)
	tag => the uid of the person to be tagged
	tag_content => the tag string (or person's name)
	tag_x => the x pct of the center of the tag
	tag_y => the y pct of the center of the tag
	
	*/
	
	// We are tagging now
	$fp = new FP_Tagger();
	$add = $fp->add_tags();
	
	if ($add == false) {
		$return = array(
			'success' => 0,
			'title' => 'Uh Ohs!',
			'fbml_content' => 'Something went wrong. Please try again...',
			'button' => 'Close'
		);
	}
	else {
		$return = $add;
	}
	echo json_encode($return);
	
}
// Remove a tag from the photo
else if ($method == 'remove_tag') {
	
	/*
	These are the things that we should be getting posted...
	
	method => remove_tag
	uid => the uid of the photo's owner
	pid => the photo id
	token => sha512(owner_uid.pid.secret.tag_ids)
	tag_ids => comma separated list of tag ids
	
	*/
	
	// We are tagging now
	$fp = new FP_Tagger();
	$remove = $fp->remove_tags();
	
	if ($remove == false) {
		$return = array(
			'success' => 0,
			'title' => 'Uh Ohs!',
			'fbml_content' => 'Something went wrong. Please try again...',
			'button' => 'Close'
		);
	}
	else {
		$return = $remove;
	}
	echo json_encode($return);
	
}
// Get tag string
else if ($method == 'refresh_tag_list') {
	
	/*
	This is what we should get...
	
	method => refresh_tag_list
	uid => the uid of the photo's owner
	pid => the photo id
	
	*/
	
	$fp = new FP_Tagger();
	$tagged_users = FP_Photo::get_tagged_users($fp->pid, $fp->config['flickr_token']);
	$tagged_users_string = FP_Photo::get_tagged_users_string($fp->uid, $fp->pid, $tagged_users);
	$tagged_users_string = str_replace('{canvasURL}', $fp->canvas_url, $tagged_users_string);
	$tagged_users_string = str_replace('{callbackURL}', $fp->callback_url, $tagged_users_string);
	
	echo $tagged_users_string;
	
}
else if ($method == 'profile_tab_photos') {
	
	$fp = new FP_ProfileTab();
	$photos = $fp->get_tagged_photos_of($fp->uid);
	
	echo '<table id="photo_album"><tr>';
	$i = 0;
	foreach($photos['photo'] as $photo) {
		echo '<td class="photo_cell">';
		echo '<a href="photos'.$fp->get_uid_from_nsid($photo['owner']).'/'.$photo['id'].'/of/'.$fp->uid.'/">';
		if ($photo['media'] == 'video') {
			echo '<img src="http://l.yimg.com/www.flickr.com/images/video_play_icon_small.png.v1" class="video_icon" />';
		}
		echo '<img class="photo_small photo_link" src="'.$fp->flickr->buildPhotoUrl($photo, 'small').'" alt="'.$photo['title'].'" title="'.$photo['title'].'" />';
		echo '</a></td>';
		$i++;
		if ($i > 4) {
			echo '</tr><tr>';
			$i = 0;
		}
	}
	echo '</tr></table>';
	
}

?>